<?php
namespace Manage\Controller;
use Think\Controller;

class CellphoneController extends CommonController{
	
	public function index(){
		
		$where['status'] = 1;
		$count = M('cellphone')->where($where)->count();
		$page = new \Common\Lib\Page($count, 30);
		$page->rollPage = 7;
		$page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
		$limit = $page->firstRow. ',' .$page->listRows;
		$list = M('cellphone')->where($where)->limit($limit)->select();
		$pages = $page->show();
		$this->assign('page', $pages);
		$this->assign('cellphone', $list);
		$this -> display();
	}
	
	public function cellphone(){
		$where['status'] = 1;
		$cellphone = M('cellphone')->where($where)->select();
		return $cellphone;
	}
	
  public function expCellphone()
    {	
    	$cellphone = $this->cellphone();
        $data = array();
        foreach ($cellphone as $k=>$v){
            $data[$k][number] = $v['number'];
            $data[$k][package] = $v['package'];
            $data[$k][hall] = $v['hall'];
            $data[$k][payment] = $v['payment'];
            $data[$k][username]  = $v['username'];
            $data[$k][contactphone]  = $v['contactphone'];
            $data[$k][idcard]  = $v['idcard'];
            $data[$k][statustime]  = $v['statustime'];
        }
       
        foreach ($data as $field=>$v){
	            if($field == 'number'){
	                $headArr[]='手机号';
	            }
	            if($field == 'package'){
	                $headArr[]='套餐';
	            }
	
	            if($field == 'hall'){
	                $headArr[]='营业厅';
	            }
	
	            if($field == 'payment'){
	                $headArr[]='预存话费';
	            }
	
	            if($field == 'username'){
	                $headArr[]='姓名';
	            }
	
	            if($field == 'contactphone'){
	                $headArr[]='联系电话';
	            }
	
	            if($field == 'idcard'){
	                $headArr[]='身份证';
	            }
	            
	            if($field == 'statustime'){
	                $headArr[]='订单时间';
	            }
        }
       
        $filename="Cellphone";
        $this->getExcel($filename,$headArr,$data);
    }


    private function getExcel($fileName,$headArr,$data){

    	import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");

        $date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";

        //创建PHPExcel对象
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
		
     //设置表头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $key += 1;
        }

        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
       
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }
        $fileName = iconv("utf-8", "gb2312", $fileName);
        
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
       
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
		
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载 
        exit;
    }
}